<template>
	<view class="display flow">
		<block v-if="isShopOrder">
			<button class="orderBtn themeBgColor btn2" v-if="orderList.status == 1" @click="deliveryBtn">发货</button>
		</block>
		<block v-else>
			<button class="orderBtn themeBgColor" v-if="orderList.status == 0 || orderList.status == 3 || orderList.status == 4" @click="deleBtn">{{btn1Txt}}</button>
			<button class="orderBtn themeBgColor" v-if="orderList.status == 2" @click="logisticsClick">查看物流</button>
			<block v-if="!isClose">
				<button class="orderBtn themeBgColor btn2" v-if="orderList.status == 0 || orderList.status == 1 || orderList.status == 2" @click="clickBtn">{{btn2Txt}}</button>
			</block>
			<button class="orderBtn themeBgColor btn2" v-if="orderList.status == 3 && orderList.tell_status == 0 && orderList.tk_status == 0" @click="evaluationBtn">写评价</button>	
		</block>
		<uni-popup ref="showdelivery" :type="popType" @change="deliveryChange">
			<deliveryAre :orderList="orderList" @deliveryOk="deliveryOk" @deliveryCancel="deliveryCancel"></deliveryAre>
		</uni-popup>
	</view>
</template>

<script>
	import util from '../common/util.js'
	import deliveryAre from '@/components/deliveryAre.vue'
	export default {
		name: 'orderBtnAre',
		props: ['orderList','shopType','orderBtnAre','isShopOrder','isClose'],
		components: {deliveryAre},
		data() {
			return {
				popType: '',
				fzIndex: 0
			};
		},
		computed:{
			btn1Txt(){
				let text = this.orderList.status=='0'?'取消订单':'删除订单'
				return text
			},
			btn2Txt(){
				let text = ''
				switch (this.orderList.status){
					case '0':
						text = '立即支付'
						break;
					case '1':
						text = '提醒发货'
						break;
					case '2':
						text = '确认收货'
						break;
				}
				return text
			},
		},
		methods:{
			deleBtn(){
				let type = this.shopType==0?1:2
				util.showModalAll('删除订单','您是否确定删除所选订单？',()=>{
					util.getAjax(this.global.apiUrl + '/user_delete_supplier_order',(res)=>{
						console.log(res)
						if(res.data.code == 200){
							util.showToast('none','删除订单成功')
							this.$emit('deleBtn')
						}else{
							util.showToast('none','删除订单失败，请重试')
						}
					},{order_id: this.orderList.id,type: type})
				})
			},
			clickBtn(){
				switch (this.orderList.status){
					case '0':
						this.payClick()
						break
					case '1':
						util.showToast('none','已通知商家发货，请您耐心等待')
						break;
					case '2':
						let url = this.shopType==0?'/user_yes_supplier_order':'/user_yes_user_shop_order'
						util.getAjax(this.global.apiUrl + url,(res)=>{
							console.log(res)
							if(res.data.code == 200){
								util.showToast('none','确认收货成功')
								this.$emit('deleBtn')
							}else{
								util.showToast('none','操作失败，请重试')
							}
						},{order_id: this.orderList.id})
						break;
				}
			},
			getIsClose(closetime){
				let isClose = false
				let start = new Date().getTime()
				let close = util.calculate(closetime,1000)
				if(start > close){
					isClose = true
				}
				return isClose
			},
			payClick(){
				var that = this
				if(that.orderList.status == 0 && that.getIsClose(that.orderList.closetime)){
					util.showToast('none','订单已超时，请重新下单');
					return
				}
				var orderNum = that.shopType==0?that.orderList.all_order_num:that.orderList.orderNum
				var url = that.shopType==0?'/supplier_product_order_ok':'/user_shop_product_order_ok'
				uni.requestPayment({
					timeStamp: that.orderList.timestamp,
					nonceStr: that.orderList.noncestr,
					package: that.orderList.prepay_id,
					signType: 'MD5',
					paySign: that.orderList.paysign,
					success(pay){
						console.log(pay);
						if(pay.errMsg == 'requestPayment:ok'){
							that.orderQuery(orderNum,url,'支付成功！')
						}
					},
					fail(payErr){
						if(payErr.errMsg == 'requestPayment:fail cancel'){
							that.orderQuery(orderNum,url,'已支付')
							// var orderNum = that.shopType==0?that.orderList.all_order_num:that.orderList.orderNum
							// util.getAjax(that.global.apiUrl + '/orderquery',(res)=>{
							// 	console.log(res);
							// 	if(res.data){
							// 		if(res.data.trade_state == 'SUCCESS'){
							// 			util.postAjax(that.global.apiUrl + url,function(res){
							// 				console.log(res)
							// 				if(res.data.code == 1){
							// 					that.$emit('payOk')
							// 				}
							// 			},{all_order_num: orderNum})
							// 		}
							// 	}
							// },{all_order_num: orderNum})
						}
					}
				})
			},
			orderQuery(orderNum,url,payText){
				var that = this
				util.getAjax(that.global.apiUrl + '/orderquery',(res)=>{
					console.log(res);
					if(res.data){
						if(res.data.trade_state == 'SUCCESS'){
							if(that.shopType == 0){
								let transactionId = res.data.transaction_id
								util.postAjax(that.global.apiUrl + url,function(res){
									// console.log(res)
									if(that.orderList.profit_sharing == 1 && that.orderList.is_fx == 1){
										that.fashionable()
									}
									util.showToast('success',payText)
									that.$emit('payOk')
								},{all_order_num: orderNum,transaction_id: transactionId})
							}else{
								util.postAjax(that.global.apiUrl + url,function(res){
									console.log(res)
									if(res.data.code == 1){
										util.showToast('success',payText)
										that.$emit('payOk')
									}
								},{order_num: orderNum})
							}
						}
					}
				},{all_order_num: orderNum})
			},
			//分账
			fashionable(){
				setTimeout(()=>{
					util.postAjax(this.global.apiUrl + '/multiprofitsharing',(res)=>{
						console.log(res);
						this.fzIndex ++ 
						if(res.data.return_code == 'FAIL' ){
							if(this.fzIndex == 2){
								return
							}
							this.fashionable()
						}else{
							if(res.data.err_code && res.data.err_code == 'ORDER_NOT_READY'){
								if(this.fzIndex == 2){
									return
								}
								this.fashionable()
							} 
						}
					},{order_num: this.orderNum})
				},2000)
			},
			deliveryBtn(){
				this.toggleDeliveryPopup('bottom', 'delivery')
			},
			toggleDeliveryPopup(type, open) {
				this.popType = type
				this.$nextTick(() => {
					this.$refs['show' + open].open()
				})
			},
			deliveryCancel(type) {
				this.$refs['show' + type].close()
			},
			deliveryChange(e) {
				console.log('是否打开:' + e.show)
			},
			deliveryOk(type){
				this.$refs['show' + type].close()
				this.$emit('deliveryOk')
			},
			evaluationBtn(){
				uni.navigateTo({
					url: '/subPackages/mine/writeEvaluationAre/writeEvaluationAre?id=' + this.orderList.id + '&shopType=' + this.shopType
				})
			},
			logisticsClick(){
				this.$emit('logisticsClick',this.orderList)
			},
		}
	}
</script>

<style lang="scss">
	.btn2{
		@include margin(20rpx,'left');
	}
</style>
